﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Group3.MediaTool.Interface;
using Group3.Framework.Extensions;
using Group3.MediaTool.Data.IO;

namespace Group3.MediaTool.Provider
{
    /// <summary>
    /// @Version 1.0
    /// @Author Pascal Senjic
    /// @Comment
    ///     This is the class for interacting with the dynamic objects
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class InstanceReader{
        /// <summary>
        /// This represents a generel way of setting a value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="orm"></param>
        /// <param name="propertyname"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static bool SetValue<T>(this T orm, string propertyname, object val) where T : IObjectRelationModel
    
        {
            try
            {
                if (val != null)
                {
                    orm.GetType().GetProperty(propertyname).SetValue(orm, val);
                }
                return true;
            }
            catch (Exception e)
            {
                DataDefinitionIO ddIO = new DataDefinitionIO();
                ddIO.writeLogFile(e.ToString(), "Error");
                return false;
            }
        }
        /// <summary>
        /// Gets all Propertynames of an object
        /// </summary>
        /// <param name="orm"></param>
        /// <returns></returns>
        public static IEnumerable<string> GetPropertyNames<T>(this T orm)  where T: IObjectRelationModel   
        {
            try
            {
                var arr = orm.GetType().GetProperties();
                if (arr != null)
                {
                    var lst = arr.Select(s => s.Name);
                    return lst;
                }
                return null;
            }
            catch (Exception e)
            {
                DataDefinitionIO ddIO = new DataDefinitionIO();
                ddIO.writeLogFile(e.ToString(), "Error");
                return null;
            }
        }
        /// <summary>
        /// Returns a value of a specific property
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="orm"></param>
        /// <param name="propertyname"></param>
        /// <returns></returns>
        public static object GetValue<T>(this T orm, string propertyname) where T : IObjectRelationModel
        {
            try
            {
                var res = orm.GetType().GetProperty(propertyname).GetValue(orm);
                return res;
            }
            catch (Exception e)
            {
                DataDefinitionIO ddIO = new DataDefinitionIO();
                ddIO.writeLogFile(e.ToString(), "Error");
                return null;
            }
        }
    }
}
